package com.game.primitive.context;

import com.game.config.primitive.PrimitiveConfig;
import com.game.config.primitive.RoomConfig.RoomCfgBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * Created by lizhiwei on 2016/7/6.
 */
public class Room extends AbstractLoop {
	private Logger logger = LoggerFactory.getLogger(PrimitiveConfig.class);
	private int id;
	private RoomCfgBean roomCfgBean;

	private Map<Integer, Battlefield> battlefieldMap = new ConcurrentHashMap<Integer, Battlefield>();

	public void loop(long now) {
		Iterator<Battlefield> iterator = battlefieldMap.values().iterator();
		while (iterator.hasNext()) {
			Battlefield next = iterator.next();
			if (next.isCanDestroy()) {
				iterator.remove();
			}
		}
		if (battlefieldMap.isEmpty()) {
			for (int i = 0; i < 99900; i++) {
				int id = IdGen.getId();
				battlefieldMap.put(id,new Battlefield());
			}
		}
		for (Battlefield battlefield : battlefieldMap.values()) {
			try {
				battlefield.loop(now);
			} catch (Exception e) {
				logger.error(String.format("%s loop error",battlefield),e);
			}
		}
	}

	public RoomCfgBean getPrimitiveRoom() {
		return roomCfgBean;
	}

	public void setRoomCfgBean(RoomCfgBean primitiveRoom) {
		this.roomCfgBean = primitiveRoom;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}
